package com.base.cn.platform.os.controller.order.pay;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.order.OrderStatus;
import com.base.cn.platform.os.common.utils.order.PayType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.order.PayCallbackEntity;
import com.base.cn.platform.os.entity.order.order.Order;
import com.base.cn.platform.os.entity.order.order.OrderType;
import com.base.cn.platform.os.entity.user.account.CusUserAccount;
import com.base.cn.platform.os.service.order.order.OrderBiz;
import com.base.cn.platform.os.service.user.account.CusUserAccountBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 支付接口
 *
 * @author s.li
 * @create 2018-09-11-18:27
 */
@RestController
@RequestMapping("/manage/order")
public class PayOrderCallbackController extends BaseController {

    @Autowired
    private OrderBiz orderBiz;
    @Autowired
    private CusUserAccountBiz cusUserAccountBiz;

    /**
     * 支付成功回调
     * @param payCallbackEntity 回调数据
     * @return Map<String,Object>
     */
    @RequestMapping("/payCallback")
    public Map<String,Object> payCallback(@RequestBody PayCallbackEntity payCallbackEntity) throws Exception{
        if(!PayType.PAY_TYPE_ALIPAY.equals(payCallbackEntity.getPayType())
                && !PayType.PAY_TYPE_WEIXIN.equals(payCallbackEntity.getPayType())
                && !PayType.PAY_TYPE_ACCOUNT.equals(payCallbackEntity.getPayType()) &&
                !PayType.PAY_TYPE_IOS.equals(payCallbackEntity.getPayType())){
            return ResultUtil.ERROR("支付方式错误");
        }
        //获取订单号
        Order order = orderBiz.findOrderByOrderNo(payCallbackEntity.getOrderNo(),false,true,true,false);
        if(payCallbackEntity.getOrderType().equals(OrderType.COMMON.getKey())){
            if(ObjectUtils.isEmpty(order)){
                return ResultUtil.ERROR("该订单不存在，支付失败");
            }
            if(payCallbackEntity.getUserId().compareTo(order.getUserId()) != 0){
                return ResultUtil.ERROR("不能操作不属于自己的订单数据");
            }
        }
        if(payCallbackEntity.getOrderType().equals(OrderType.ACCOUNT.getKey())){//账户充值
            if(ObjectUtils.isNotEmpty(order)){
                return ResultUtil.SUCCESS("账户充值成功",order);
            }
            //创建账户充值订单数据
            order = orderBiz.createAccountOrder(payCallbackEntity);
            return ResultUtil.SUCCESS("账户充值成功",order);
        } else if(payCallbackEntity.getOrderType().equals(OrderType.COMMON.getKey())){//普通商品订单

            if(order.getOrderStatus().equals(OrderStatus.SUCCESS)){
                return ResultUtil.ERROR("订单支付成功",order);
            }
            if(payCallbackEntity.getPayType().equals(PayType.PAY_TYPE_ACCOUNT)){//账户支付类型，验证账户金额是否足够
                CusUserAccount userAccount = cusUserAccountBiz.queryUserAccountByUserId(order.getUserId(),false);
                double balance = userAccount.getBalance().doubleValue();
                if(order.getRealPrice() > balance){
                    return ResultUtil.ERROR("账户余额不足，支付失败");
                }
            }
            order.setPayType(payCallbackEntity.getPayType());
            order.setExternalOrderNo(payCallbackEntity.getExternalOrderNo());
            order.setOrderStatus(OrderStatus.SUCCESS);
            order.setPayTime(new Date());
            if(StringUtils.isNotEmpty(payCallbackEntity.getOrderForm())){
                order.setOrderForm(payCallbackEntity.getOrderForm());
            }
            //更新订单状态
            orderBiz.updateOrderStatus(order, BigDecimal.ZERO);
            return ResultUtil.SUCCESS("订单支付成功",order);
        }
        return ResultUtil.ERROR("订单数据错误，支付失败");
    }
}
